<?php
/**
 * Class that operate on table 'hs_location'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2012-05-30 11:06
 */
class HsLocationMySqlDAO implements HsLocationDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return HsLocationMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM hs_location WHERE locationID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM hs_location';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM hs_location ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param hsLocation primary key
 	 */
	public function delete($locationID){
		$sql = 'DELETE FROM hs_location WHERE locationID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($locationID);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param HsLocationMySql hsLocation
 	 */
	public function insert($hsLocation){
		$sql = 'INSERT INTO hs_location (userID, locationCountry, locationState, locationCity, locationAddr1, locationAddr2, zipCode) VALUES (?, ?, ?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsLocation->userID);
		$sqlQuery->set($hsLocation->locationCountry);
		$sqlQuery->set($hsLocation->locationState);
		$sqlQuery->set($hsLocation->locationCity);
		$sqlQuery->set($hsLocation->locationAddr1);
		$sqlQuery->set($hsLocation->locationAddr2);
		$sqlQuery->set($hsLocation->zipCode);

		$id = $this->executeInsert($sqlQuery);	
		$hsLocation->locationID = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param HsLocationMySql hsLocation
 	 */
	public function update($hsLocation){
		$sql = 'UPDATE hs_location SET userID = ?, locationCountry = ?, locationState = ?, locationCity = ?, locationAddr1 = ?, locationAddr2 = ?, zipCode = ? WHERE locationID = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($hsLocation->userID);
		$sqlQuery->set($hsLocation->locationCountry);
		$sqlQuery->set($hsLocation->locationState);
		$sqlQuery->set($hsLocation->locationCity);
		$sqlQuery->set($hsLocation->locationAddr1);
		$sqlQuery->set($hsLocation->locationAddr2);
		$sqlQuery->set($hsLocation->zipCode);

		$sqlQuery->set($hsLocation->locationID);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM hs_location';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByUserID($value){
		$sql = 'SELECT * FROM hs_location WHERE userID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLocationCountry($value){
		$sql = 'SELECT * FROM hs_location WHERE locationCountry = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLocationState($value){
		$sql = 'SELECT * FROM hs_location WHERE locationState = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLocationCity($value){
		$sql = 'SELECT * FROM hs_location WHERE locationCity = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLocationAddr1($value){
		$sql = 'SELECT * FROM hs_location WHERE locationAddr1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLocationAddr2($value){
		$sql = 'SELECT * FROM hs_location WHERE locationAddr2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByZipCode($value){
		$sql = 'SELECT * FROM hs_location WHERE zipCode = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByUserID($value){
		$sql = 'DELETE FROM hs_location WHERE userID = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLocationCountry($value){
		$sql = 'DELETE FROM hs_location WHERE locationCountry = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLocationState($value){
		$sql = 'DELETE FROM hs_location WHERE locationState = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLocationCity($value){
		$sql = 'DELETE FROM hs_location WHERE locationCity = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLocationAddr1($value){
		$sql = 'DELETE FROM hs_location WHERE locationAddr1 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLocationAddr2($value){
		$sql = 'DELETE FROM hs_location WHERE locationAddr2 = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByZipCode($value){
		$sql = 'DELETE FROM hs_location WHERE zipCode = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return HsLocationMySql 
	 */
	protected function readRow($row){
		$hsLocation = new HsLocation();
		
		$hsLocation->locationID = $row['locationID'];
		$hsLocation->userID = $row['userID'];
		$hsLocation->locationCountry = $row['locationCountry'];
		$hsLocation->locationState = $row['locationState'];
		$hsLocation->locationCity = $row['locationCity'];
		$hsLocation->locationAddr1 = $row['locationAddr1'];
		$hsLocation->locationAddr2 = $row['locationAddr2'];
		$hsLocation->zipCode = $row['zipCode'];

		return $hsLocation;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return HsLocationMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>